package top.appx;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import sun.org.mozilla.javascript.internal.json.JsonParser;
import top.appx.Util;

import java.io.*;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

/**
 * Created by qq799 on 2017/1/14.
 */
public class T {
    public static String articleDic = "C:\\Users\\qq799\\OneDrive\\workspace\\idea\\blog\\web\\article\\";
    public static  void add(String title,String content, Timestamp createTime)throws Exception{
        System.out.println("路径："+articleDic);
        String templet_articlePath = articleDic+"templet_article.html";
        String templet_articleList = articleDic+"templet_articleList.html";
        String articleListJsonPath = articleDic+"articleList.json";

        System.out.println("aaa:"+articleListJsonPath);
        File file = new File(articleListJsonPath);
        JSONObject json = JSON.parseObject(Util.readFile(file));
        JSONArray list = json.getJSONArray("list");


        int id = 1;
        if(list.size()>0){
            JSONObject j = (JSONObject)list.get(list.size()-1);
            id = j.getInteger("id")+1;
        }
        //#region 创建文章

        Document doc = Jsoup.parse(Util.readFile(templet_articlePath));
        doc.title(title);
        doc.getElementById("title").html(title);
        doc.getElementById("content").html(content);



        Util.writeFile(articleDic+"articleItem\\article_"+id+".html",doc.html());

        //#endregion 创建文章



        //#region 将json增加后覆盖
        JSONObject obj = new JSONObject();
        obj.put("id",id);
        obj.put("title",title);
        obj.put("createTime",createTime);
        list.add(obj);

        Util.writeFile(file.getAbsolutePath(),json.toJSONString());
        //#endregion 将json增加后覆盖


        String templet_articleText = Util.readFile(templet_articlePath);
        String templet_articleListText= Util.readFile(templet_articleList);

        int pageSize = 20;
        int ps = (int)Math.ceil(list.size()*1.0/pageSize);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        for(int p=0;p<ps;p++){
             doc = Jsoup.parse(templet_articleListText);
            //#region 文章列表
            Element tbody = doc.getElementById("tbody_articleList");
            String str = "";
            for (int i=0;i<pageSize;i++){
                int index = list.size() - p*pageSize-i-1;
                if(index<0){
                    continue;
                }
                JSONObject article = list.getJSONObject(index);
                String t = article.getString("title");
                Timestamp cr = article.getTimestamp("createTime");
                int idtmp = article.getInteger("id");
                String createTimeStr = sdf.format(cr);
                str +="<tr><td><a target=\"_blank\" href='/article/articleItem/article_"+idtmp+".html'>"+t+"</a></td><td>"+createTimeStr+"</td></tr>";
            }

            tbody.html(str);

            //#endregion 文章列表


            //#region 分页
            Element page_pre = doc.getElementById("page_pre");
            Element page_next = doc.getElementById("page_next");

            if(p==0){
                page_pre.addClass("disabled");
            }else{
                page_pre.child(0).attr("href","/article/articleList/articleList"+(p==1?"":("_"+p))+".html");
            }

            if(p+1==ps){
                page_next.addClass("disabled");
            }else{
                page_next.child(0).attr("href","/article/articleList/articleList"+(p+1==ps?"":("_"+(p+2)))+".html");
            }

            for(int p_0=0;p_0<ps;p_0++){
                page_next.before("<li class='"+(p==p_0?"active":"")+"'><a href='/article/articleList/articleList"+(p_0==0?"":("_"+(p_0+1)))+".html'>"+(p_0+1)+"</a></li>");
            }
            //#endregion 分页

            String path =articleDic+"articleList\\articleList"+(p==0?"":("_"+(p+1)))+".html";
            Util.writeFile(path,doc.html());
        }
        System.out.println("succ");
    }

}
